//
//  ScreenView.m
//  ZiJinLian
//
//  Created by 🐛 on 2017/4/24.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "ScreenView.h"

@implementation ScreenView

@synthesize alphaView,whiteView,headImage,LB_detail,LB_line,LB_price,LB_stock,LB_showSales,mainscrollview,cancelBtn,addBtn,againBtn,stockBtn,minPrince,maxPrince;
-(instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.backgroundColor = [UIColor clearColor];
        
        [self creatUI];
        
    }
    return self;
}

-(void)creatUI{
    CGFloat height=MainW/2-60;
    CGFloat width=(MainW-155)/2;
    //半透明视图
    alphaView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MainW, MainH)];
    alphaView.backgroundColor = [UIColor clearColor];
    alphaView.alpha = 0.4;
    [self addSubview:alphaView];
    
    //装载商品信息的视图
    whiteView = [[UIView alloc] initWithFrame:CGRectMake(120, 0, MainW-120, MainH-64)];
    whiteView.backgroundColor = [UIColor whiteColor];
    [self addSubview:whiteView];
    
    
    cancelBtn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 120, MainH-64)];
    [cancelBtn setBackgroundColor:[UIColor clearColor]];
    [cancelBtn addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:cancelBtn];
    mainscrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, MainW-120, MainH-64-49)];
    mainscrollview.backgroundColor = [UIColor clearColor];
    mainscrollview.contentSize = CGSizeMake(0, 200);
    mainscrollview.showsHorizontalScrollIndicator = NO;
    mainscrollview.showsVerticalScrollIndicator = NO;
    [whiteView addSubview:mainscrollview];
    LB_price=[[MYLabel alloc]initWithFrame:CGRectMake(10, 10, MainW-120-50, 15)];
    LB_price.text=@"价格区间 (元)";
    LB_price.textColor=TextColor3;
    LB_price.backgroundColor=[UIColor clearColor];
    [mainscrollview addSubview:LB_price];
    //取消按钮
    UIImageView*imag=[[UIImageView alloc]initWithFrame:CGRectMake(MainW-145, 10, 15, 15)];
    imag.image=[UIImage imageNamed:@"icon_cross"];
    [mainscrollview addSubview:imag];
    stockBtn=[[UIButton alloc]initWithFrame:CGRectMake(MainW-160, 0, 40, 35)];
    //stockBtn.backgroundColor=[UIColor redColor];
    [stockBtn addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
    [mainscrollview addSubview:stockBtn];
    //最低价格
    minPrince=[[UITextField alloc]initWithFrame:CGRectMake(10, LB_price.bsg_bottom+10, width, 25)];
    minPrince.placeholder=@"最低价";
    minPrince.delegate=self;
    minPrince.textColor=TextColor6;
    minPrince.keyboardType=UIKeyboardTypeNumberPad;
    minPrince.font=FontAndStyle14;
    minPrince.textAlignment=NSTextAlignmentCenter;
    //[minPrince setValue:NSTextAlignmentCenter forKeyPath:@"_placeholderLabel.textColor"];
    [minPrince setValue:FontAndStyle12 forKeyPath:@"_placeholderLabel.font"];
    minPrince.backgroundColor=SeparatorColor;
    ViewRadius(minPrince, 2);
    [mainscrollview addSubview:minPrince];
    UIView*v=[[UIView alloc]initWithFrame:CGRectMake(minPrince.bsg_right+2, minPrince.bsg_centerY, 11, 1)];
    v.backgroundColor=SeparatorColor;
    [mainscrollview addSubview:v];
    //最高价格
    maxPrince=[[UITextField alloc]initWithFrame:CGRectMake(minPrince.bsg_right+15, LB_price.bsg_bottom+10, width, 25)];
    maxPrince.keyboardType=UIKeyboardTypeNumberPad;
    maxPrince.textAlignment=NSTextAlignmentCenter;
    maxPrince.font=FontAndStyle14;
    maxPrince.placeholder=@"最高价";
    maxPrince.textColor=TextColor6;
    [maxPrince setValue:FontAndStyle12 forKeyPath:@"_placeholderLabel.font"];
    maxPrince.backgroundColor=SeparatorColor;
    ViewRadius(maxPrince, 2);
    [mainscrollview addSubview:maxPrince];
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, maxPrince.bsg_bottom+10, MainW-120, 10)];
    view.backgroundColor=Gray_Color;
    [mainscrollview addSubview:view];
    againBtn=[[UIButton alloc]initWithFrame:CGRectMake(0, MainH-64-49, height, 49)];
    [againBtn setTitle:@"重置" forState:UIControlStateNormal];
    [againBtn setTitleColor:TextColor3 forState:UIControlStateNormal];
    [againBtn setBackgroundColor:SeparatorColor];
    [againBtn addTarget:self action:@selector(againBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [whiteView addSubview:againBtn];
    addBtn=[[UIButton alloc]initWithFrame:CGRectMake(height, MainH-64-49, height, 49)];
    [addBtn setTitle:@"确定" forState:UIControlStateNormal];
    [addBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [addBtn setBackgroundColor:button_Red_Color];
    [addBtn addTarget:self action:@selector(sureBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [whiteView addSubview:addBtn];
}
-(void)cancelClick{
    [_delegate CancelBtnClcik];
}
-(void)againBtnClick{
    maxPrince.text=nil;
    minPrince.text=nil;
    [_delegate againBtnClick];
}
-(void)sureBtnClick{
    [_delegate sureBtnClick];
}
//- (UIToolbar *)addToolbar
//{
//    
//    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, MainW, 50)];
//    //    UIToolbar *toolbar =[[UIToolbar alloc] init];
//    UIBarButtonItem *bar = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(numberFieldCancle)];
//    toolbar.items = @[bar];
//    
//    return toolbar;
//}
-(UIToolbar *)addToolbar
{
    
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, MainW, 50)];
    //    UIToolbar *toolbar =[[UIToolbar alloc] init];
    UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
   UIBarButtonItem *bar = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(textFieldDone)];
    toolbar.items = @[space,bar];
    toolbar.tintColor=Red_Color;
    
    return toolbar;
}
-(void)textFieldDone{
    [self endEditing:YES];
    if ([self.minPrince.text intValue]>[self.maxPrince.text intValue]) {
        NSString* a=self.maxPrince.text;
        self.maxPrince.text=self.minPrince.text;
        self.minPrince.text=a;
    }
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    //if (textField==self.minPrince) {
        self.maxPrince.inputAccessoryView=[self addToolbar];
        self.minPrince.inputAccessoryView=[self addToolbar];
    //}
   
    return YES;
}
@end
